//
//  HomeView.swift
//  jiaoyi
//
//  Created by wjk on 2018/11/6.
//  Copyright © 2018 wjk. All rights reserved.
//

import UIKit


extension String {
    /// range转换为NSRange
    func nsRange(from range: Range<String.Index>) -> NSRange {
        return NSRange(range, in: self)
    }
}

class HomeView: UIView, UITextViewDelegate {

    @IBOutlet weak var topView: UIView!
    
    var ob:okBlock?
    var can:okBlock?
    
    var nav:UINavigationController?
    @IBOutlet weak var textView: UITextView!
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    public static func showView(frame:CGRect) ->HomeView {
        let view = Bundle.main.loadNibNamed("HomeView", owner: nil, options: nil)?.first as! HomeView
        view.frame = frame
        
        
        let str = "您是否已阅读并承诺遵守《交易规则》、当期《交易公告》等相关规定"
        let s = NSMutableAttributedString.init(string:str);
        s.addAttributes([NSAttributedStringKey.font:UIFont.systemFont(ofSize: 16)], range: str.nsRange(from: str.range(of: str)!))
        s.addAttributes([NSAttributedStringKey.link:"healthservice://"], range: str.nsRange(from: str.range(of: "《交易规则》")!))

        s.addAttributes([NSAttributedStringKey.foregroundColor:kRGBColorFromHex(rgbValue: 0xEF8974)], range:str.nsRange(from: str.range(of: "《交易规则》")!) )
        
        s.addAttributes([NSAttributedStringKey.link:"digitalcer://"], range: str.nsRange(from: str.range(of: "《交易公告》")!))

        
        s.addAttributes([NSAttributedStringKey.foregroundColor:kRGBColorFromHex(rgbValue: 0xEF8974)], range: str.nsRange(from: str.range(of: "《交易公告》")!))
        
        
        
        view.textView.attributedText = s
        view.textView.delegate = view
        return view
    }
    
    
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
        if URL.scheme?.contains("healthservice") == true {
            let web = WebViewController()
            web.urlArr = ["http://www.grain.cq.cn/article/17/"]
            web.title = "交易规则"
            self.nav?.pushViewController(web, animated: true)
            
            return false
        }else if URL.scheme?.contains("digitalcer") == true {
            
            let web = WebViewController()
            web.title = "交易公告"
            web.urlArr = ["http://www.grain.cq.cn/article/15/"]
            self.nav?.pushViewController(web, animated: true)
            
            return false
        }
        return true
    }
    
    @IBAction func calcle(_ sender: UIButton) {
        self.removeFromSuperview()
        self.can?(["":""])
    }
    @IBAction func ok(_ sender: UIButton) {
        self.ob?(["":""])
        self.removeFromSuperview()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.removeFromSuperview()
    }
}
